﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace McCloudCore
{
    public class McTableEntity : TableStorageEntity
    {
        public string key { get; set; }
        public string codein { get; set; }
        public string codeout { get; set; }
        public double interactions { get; set; }
        public double tasks { get; set; }
        public double instancesadded { get; set; }
        public int status { get; set; }
        public int timeout { get; set; }
        public double durationsplit { get; set; }
        public double durationprocess { get; set; }        
        public double durationmerge { get; set; }
        
        public McTableEntity() : base() { PartitionKey = String.Empty; RowKey = Guid.NewGuid().ToString(); this.Start(); }
        public McTableEntity(string partitionKey, string rowKey) : base(partitionKey, rowKey) { this.Start(); }

        private static McTableModel data = null;
        
        private void Start()
        {
            StorageAccountInfo account = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
            data = new McTableModel(account);
            data.RetryPolicy = Microsoft.Samples.ServiceHosting.StorageClient.RetryPolicies.RetryN(5, TimeSpan.FromSeconds(5));
        }

        private void TryCreate()
        {
            StorageAccountInfo account = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
            TableStorage tableStorage = TableStorage.Create(account);
            tableStorage.RetryPolicy = Microsoft.Samples.ServiceHosting.StorageClient.RetryPolicies.RetryN(5, TimeSpan.FromSeconds(5));
            TableStorage.CreateTablesFromModel(typeof(McTableModel), account);
        }

        public void Save()
        {
            this.TryCreate();
            data.AddObject(data.name, this);            
            data.SaveChanges();
        }

        public void Update()
        {            
            data.UpdateObject(this);
            data.SaveChanges();
        }

        public bool Load()
        {
            if (data == null) return false;
            if (this.key == null) return false;
            if (this.key == string.Empty) return false;

            var results = (from aux in data.mcc
                           where aux.key == key
                           select aux).Take(1);
            TableStorageDataServiceQuery<McTableEntity> query =
                new TableStorageDataServiceQuery<McTableEntity>(results as DataServiceQuery<McTableEntity>);
            IEnumerable<McTableEntity> list = query.ExecuteAllWithRetries();
            if (list.Count() > 0)
            {
                McTableEntity item = list.First();
                this.key = item.key;
                this.codein = item.codein;
                this.codeout = item.codeout;
                this.interactions = item.interactions;
                this.tasks = item.tasks;
                this.instancesadded = item.instancesadded;
                this.status = item.status;
                this.durationprocess = item.durationprocess;
                this.durationmerge = item.durationmerge;
                this.durationsplit = item.durationsplit;
                this.PartitionKey = item.PartitionKey;
                this.RowKey = item.RowKey;
                this.Timestamp = item.Timestamp;
                this.timeout = item.timeout;
                
                data.AttachTo(data.name, this, "*");

                return true;
            }
            return false;
        }

    }
}
